//
//  registerController.swift
//  Order
//
//  Created by Lazyloading on 2020/7/6.
//  Copyright © 2020 lazyloading. All rights reserved.
//

import UIKit
import QMUIKit
import PKHUD

/// 注册
class RegisterController: UIViewController {

    @IBOutlet weak var accountField: QMUITextField!
    @IBOutlet weak var passwordField: QMUITextField!
    @IBOutlet weak var passwordField2: QMUITextField!
    @IBOutlet weak var inviteCodeField: QMUITextField!
    @IBOutlet weak var registerBtn: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.loadDefaultSetting();
        self.initSubViews();
    }
    
    // MARK: 👉🏻 🐷Life cycle🐷
    // MARK: 👉🏻 🐷System Delegate🐷
    // MARK: 👉🏻 🐷Custom Delegate🐷
    // MARK: 👉🏻 🐷Event  Response🐷
    @IBAction func registerAction(_ sender: Any) {
        let param = [
                 "username" : self.accountField.text ?? "",
                 "password" : self.passwordField2.text?.md5() ?? "",
                 "inviteCode" : self.inviteCodeField.text ?? ""
            ] as [String : Any];
        MoyaRequest(.register(param), success: { (response : BaseResponse<[String : Any]>) in
            if response.respCode == 200{
                
                HUD.flash(.label("注册成功"), delay: 1);
                DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
                    self.navigationController?.popViewController(animated: true);
                }
     
            }else{
                HUD.flash(.label(response.respMessage), delay: 1);
            }
        }) { (error) in
            HUD.flash(.label("注册失败"), delay: 1);
            
        }
    }
    
    @objc func back(sender : UIButton) -> Void {
        self.navigationController?.popViewController(animated: true);
    }
    
    @objc func textfieldValueChanged(textfield : UITextField) {
        ZMLog(textfield.text);
        
        if self.accountField.text!.count  > 0 && self.passwordField.text!.count > 0 && self.passwordField2.text!.count > 0 && self.inviteCodeField.text!.count > 0 {
            self.registerBtn.isUserInteractionEnabled = true;
            self.registerBtn.backgroundColor = ZMColor.Orange.orange1;
        }else{
            self.registerBtn.isUserInteractionEnabled = false;
            self.registerBtn.backgroundColor = ZMHexColor("e4e4e4")
        }
        
    }
    // MARK: 👉🏻 🐷Private Methods🐷
    // MARK: 👉🏻 🐷Lazy loading🐷
    // MARK: 👉🏻 🐷Init SubViews🐷
    func loadDefaultSetting()  {
        let item = self.navigationItem;
        let back = UIButton.init(type: .custom);
        back.frame = CGRect.init(x: 0, y: 0, width: 44, height: 44);
        back.setImage(ZMImage("back_black"), for: .normal);
        back.contentHorizontalAlignment = .left;
        back.addTarget(self, action: #selector(back(sender:)), for: .touchUpInside);
        item.leftBarButtonItem = UIBarButtonItem.init(customView: back);
    }
    
    func initSubViews()  {
        self.accountField.qmui_borderColor = ZMHexColor("e4e4e4");
        self.passwordField.qmui_borderColor = ZMHexColor("e4e4e4");
        self.passwordField2.qmui_borderColor = ZMHexColor("e4e4e4");
        self.inviteCodeField.qmui_borderColor = ZMHexColor("e4e4e4");
        self.accountField.qmui_borderPosition = .bottom;
        self.passwordField.qmui_borderPosition = .bottom;
        self.passwordField2.qmui_borderPosition = .bottom;
        self.inviteCodeField.qmui_borderPosition = .bottom;
        
        self.accountField.addTarget(self, action: #selector(textfieldValueChanged), for: .editingChanged);
        self.passwordField.addTarget(self, action: #selector(textfieldValueChanged), for: .editingChanged);
        self.passwordField2.addTarget(self, action: #selector(textfieldValueChanged), for: .editingChanged);
        self.inviteCodeField.addTarget(self, action: #selector(textfieldValueChanged), for: .editingChanged);
    }

}
